package com.demohomework.homeworkdemo.controller;

import com.demohomework.homeworkdemo.pojo.Admin;
import com.demohomework.homeworkdemo.pojo.Result;
import com.demohomework.homeworkdemo.util.JwtUtil;
import com.demohomework.homeworkdemo.util.Md5Util;
import com.demohomework.homeworkdemo.util.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import com.demohomework.homeworkdemo.service.AdminService;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    //添加管理员
    @PostMapping("/add")
    public Result add(@RequestBody @Validated Admin admin) {
        adminService.add(admin);
        return Result.success();
    }

    //注册
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "\\S{2,10}$") String username,@Pattern(regexp = "\\S{5,10}$") String password) {
        //查询用户
        Admin u = adminService.findbyUsername(username);
        if (u==null){
            //注册用户
            adminService.register(username, password);
            return Result.success();
        }else{
            //用户已存在
            return Result.error("用户名已存在");
        }
    }
    //登录
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "\\S{2,10}$") String username, @Pattern(regexp = "\\S{5,10}$") String password) {
        //查询用户
        Admin loginUser = adminService.findbyUsername(username);
        if (loginUser!=null){
            //验证密码
            if (Md5Util.string2MD5(password).equals(loginUser.getPassword())){
                //登录成功
                Map<String, Object> claims =new HashMap<>();
                claims.put("id",loginUser.getId());
                claims.put("username",loginUser.getUsername());
                //生成jwt token令牌
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            }else{
                //密码错误
                return Result.error("密码错误");
            }
        }else{
            //用户不存在
            return Result.error("用户不存在");
        }
    }


    //用户信息
    @GetMapping("/adminInfo")
    public Result<Admin> adminInfo(/*@RequestHeader(name = "Authorization") String token*/) {
        /*Map<String, Object> claims = JwtUtil.parseToken(token);
        System.out.println(claims);
        String username = (String) claims.get("username");*/

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        Admin admin = adminService.findbyUsername(username);
        return Result.success(admin);
    }
    //更新用户信息
    @PutMapping("/update")
    public Result update(@RequestBody @Validated Admin admin) {
        adminService.update(admin);
        return Result.success();
    }

    //修改密码
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String> params){
        //校验参数
        String oldPwd = (String) params.get("oldPwd");
        String newPwd = (String) params.get("newPwd");
        String rePwd = (String) params.get("rePwd");

        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
            return Result.error("缺少必要的参数");
        }

        //原密码验证
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Admin loginAdmin = adminService.findbyUsername(username);
        if (!Md5Util.string2MD5(oldPwd).equals(loginAdmin.getPassword())){
            return Result.error("原密码错误");
        }
        //新密码验证
        if (!newPwd.equals(rePwd)){
            return Result.error("两次密码不一致");
        }
        //修改密码
        adminService.updatePwd(newPwd);
        return Result.success();
    }

    //查看所有用户
    @GetMapping("/list")
    public Result<List<Admin>> list() {
        List<Admin> admins = adminService.list();
        return Result.success(admins);
    }
    //删除学生
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        adminService.delete(id);
        return Result.success();
    }

}

